package com.demo.shardingjdbc.service.impl;

import com.alibaba.fastjson.JSON;
import com.demo.shardingjdbc.entity.User;
import com.demo.shardingjdbc.mapper.UserMapper;
import com.demo.shardingjdbc.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.shardingjdbc.core.api.HintManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @title 用户(User)表业务实现类
 * @author hcj
 * @createDate 2018-11-22
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<User> selectSelective(User record) {
        log.debug("准备查询用户 , 请求参数 : {}", JSON.toJSONString(record));
        Set<User> users = userMapper.selectSelective(record);
        List<User> users1 = new ArrayList<>(users);
        return users1;
    }

    @Override
    public List<Map> selectSelectiveAndReturnMap(User record) {
        log.debug("准备查询用户 , 请求参数 : {}", JSON.toJSONString(record));
        Set<Map> users = userMapper.selectSelectiveAndReturnMap(record);
        List<Map> users1 = new ArrayList<>(users);
        return users1;
    }

    @Override
    public List<User> selectSelectiveMaster(User record) {
        // 强制路由主库
        HintManager.getInstance().setMasterRouteOnly();
        log.debug("强制主库查询用户 , 请求参数 : {}", JSON.toJSONString(record));
        Set<User> users = userMapper.selectSelective(record);
        List<User> users1 = new ArrayList<>(users);
        return users1;
    }

    @Override
    public int insertSelective(User record) {
        log.debug("准备创建用户 , 请求参数 : {}", JSON.toJSONString(record));
        Integer result = userMapper.insertSelective(record);
        log.debug("创建用户完成 , 影响条数 : {}" , result);
        return result;
    }

    @Override
    public int updateSelective(User record) {
        log.debug("准备更新用户 , 请求参数 : {}", JSON.toJSONString(record));
        Integer result = userMapper.updateSelective(record);
        log.debug("更新用户完成 , 影响条数 : {}" , result);
        return result;
    }

    @Override
    public int deleteById(Long userId) {
        log.debug("删除用户 , 请求参数 : {}", userId);
        Integer result = userMapper.deleteById(userId);
        log.debug("删除用户完成 , 影响条数 : {}" , result);
        return result;
    }

    @Override
    public PageInfo queryPage(User record, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> userMapper.selectSelective(record));
        return pageInfo;
    }

    @Override
    public List<Map> testLeftJoin(User record) {
        log.debug("准备查询用户 , 请求参数 : {}", JSON.toJSONString(record));
        return userMapper.testLeftJoin(record);
    }

    @Override
    public List<Map> testUnion(User record) {
        log.debug("准备查询用户 , 请求参数 : {}", JSON.toJSONString(record));
        return userMapper.testUnion(record);
    }

}